package org.getOffer.offer53;

import java.util.Arrays;

/**
 * @Author: wjy
 * @Date: 2021/9/23 14:08
 */
public class Solution {

    public static void main(String[] args) {

        int[] nums = {1,2,3};
        int target = 2;

        System.out.println(search(nums, target));

    }

    // 二分查找
    public static int search(int[] nums, int target) {

        int left = 0, right = nums.length-1;

        int count = 0;

        while (left < right) {
            int mid = (left+right)/2;
            if (nums[mid] >= target) {
                right = mid-1;
            }
            if (nums[mid] < target) {
                left = mid+1;
            }
        }

        while (left < nums.length && nums[left++] == target) {
            count++;
        }

        return count;

    }

}
